<?
require_once('Zend/Controller/Action.php');
require_once('Zend/Layout.php');
require_once('View/Smarty.php');
require_once('Locale/Translator.php');
require_once('Database/Database.php');
require_once('application/models/Users.php');
require_once('application/models/Categories.php');


abstract class Main_Controller_Action extends Zend_Controller_Action {

	protected $config;
	public $currentLang;
	protected $loggedUser;
	public $apps;

	public function init() {
		if ($this->getRequest()->getParam('sessionId')){
			session_id($this->getRequest()->getParam('sessionId'));
		}
		
		$this->apps = array(
			'id' => 1,
			'vatValue' => 23,
			'currency' => 'PLN'
		);
		
		
		date_default_timezone_set('Europe/Paris');
		// load configuration
		$config = new Zend_Config_Xml(dirname(__FILE__).'/../config.inc.xml', 'production');
		$registry = Zend_Registry::getInstance();
		$registry->set('config', $config);
		$this->config = $config;
		/**
		 * Database init - singleton, database config parameters are in Database constructor
		 * and they are store in config file
		 */
		try {
			$db = Database::instance();
		} catch (DatabaseException $e){
			$e->getMessage();
		}
		
		/**
		 * Database global debug on/off 1/0
		 */
		//$db->getConn()->debug = 1;

		//setup new view object and view helper - SMARTY integration
		$view = new Main_View_Smarty();
		$view->setScriptPath(BASE_PATH.$config->smarty->template_dir);
		$view->setCompilePath(BASE_PATH.$config->smarty->compile_dir);
		$view->setCachePath(BASE_PATH.$config->smarty->cache_dir);
		$view->setConfigPath(BASE_PATH.$config->smarty->config_dir);
		
		$favCount = new Zend_Session_Namespace('favCount');
		$cart = new Zend_Session_Namespace('cart');
		
		
		/**
		 * Current interfejs language - will be derived from the concrete shop configuration
		 */
		$lang = new Zend_Session_Namespace('lang');
		
		if ($this->getRequest()->has('lang')){
			$lang->lang = $this->getRequest()->getParam('lang');
		} else {
			if (!isset($lang->lang))
				$lang->lang = $this->config->locale->defaultLocale;
		}
		
		$this->currentLang = $lang->lang;
		
		/**
		 * commonLocale
		 */
		$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
		$view->assign('commonLocale', $commonLocale);
		
		/**
		 * if particular user is logged we assign its personal details directly to main view
		 * Smarty variable {$loggedUser}
		 */
		
		//debug($this->getRequest()->getParam('sessionId'));
		
		$auth = Zend_Auth::getInstance();
		
		if ($auth->hasIdentity()) {
			$this->loggedUser = unserialize($auth->getIdentity());
					
			$view->assign('loggedUser', $this->loggedUser);
			
			if ($this->loggedUser->getIsAdministrator() == 1){
				$view->assign('isAdmin', $var = true);
			} else {
				$view->assign('isAdmin', $var = false);
			}
			if ($this->loggedUser->getIsOperator() == 1){
				$view->assign('isOperator', $var = true);
			} else {
				$view->assign('isOperator', $var = false);
			}
		} else {
			$view->assign('loggedUser', $var = false);
			
			$this->loggedUser = false;
		}
		
		$view->assign('pageInfo', $var = false);
		if (is_array($cart->cart))
			$cartCount = sizeof($cart->cart);
		else 
			$cartCount = intval($cart->cart);
			
		$view->assign('cartCount', $cartCount);
				
		/**
		 * We assign a SMARTY view to variable view - this will integrate smarty template engine with zend frk
		 */
		$this->view = $view;
		
		
		/**
		 * ASSIGN CORRECT SKINSHOP ID _ BASE ON URL
		 */
		$this->view->appsId = 1;
		$this->view->selectedCategory = false;
		
		$this->view->sessionId = session_id();
		$this->view->thumbs = $config->thumbnails;
		$this->view->configStatuses = $config->statuses;
		$this->view->categories = ModelMapper::instance('Categories')->findAll();
					
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		$viewRenderer->setView($view)->setViewSuffix('tpl');

		//use layout view pattern
		$layout = Zend_Layout::startMvc();
		$layout->setViewSuffix('tpl');
		$view->assign( 'layout', $layout );
		
	}
	
	public function getLoggedUser(){
		return $this->loggedUser;
	}
	
	public function getCurrentLang(){
		return $this->currentLang;
	}
}
?>